
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  Registration;


import java.io.RandomAccessFile;
import java.sql.ResultSet;

import logger.PoolLogger;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Addrate 
{
	final String FILENAME="D:\\Register\\Item\\ItemReport.csv";
	final String OUTPUTFILENAME="D:\\Register\\Item\\debugrate.csv";
	GetIdName gid;
	String MRP="mrp";
	String SELL="sell";
	String BUY="buy";
	PoolLogger pl;
	
	public Addrate() 
	{
		//database = new DBManager();
		gid = new  GetIdName();
		MRP = gid.getId(MRP);
		BUY = gid.getId(BUY);
		SELL = gid.getId(SELL);
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void AddRate()
	{
		try 
		{	
			RandomAccessFile raf = new RandomAccessFile(FILENAME,"r");
			RandomAccessFile raf1 = new  RandomAccessFile(OUTPUTFILENAME,"rws");
			raf1.setLength(0);
			String line=raf.readLine();
			String splitline[] = line.split("\t");
			int indexofitemcode=-1;
			for(int i=0;i<splitline.length;i++)
			{
				if(splitline[i].equals("Itemcode"))
				{
					indexofitemcode = i;
				}
			}
			line = line + "\t" + "MRP" + "\t" + "SELL" + "\t" + "BUY";
			raf1.writeBytes(line+"\n");
			double mrp,sell,buy;
			mrp=sell=buy=0;
			while((line=raf.readLine())!=null)
			{
				try 
				{
					mrp=sell=buy=0;
					String split[] = line.split("\t");
					String itemcode = split[indexofitemcode];
					itemcode = gid.getId(itemcode);
					String query="select * from rate_master where item=";
					query = query + itemcode;
					
					ResultSet rs;
					String rate;
					try 
					{
						rs = DBManager.getSelect(query + " and BuySell="+MRP);
						rs.next();
						rate = rs.getString("rate");
						mrp = Double.parseDouble(rate);
					}
					catch (Exception e) 
					{
						mrp=0;
						e.printStackTrace();
					}
					
					//line = line + "\t" + rate;
					try 
					{
						rs= DBManager.getSelect(query + " and BuySell="+SELL);
						rs.next();
						rate = rs.getString("rate");
						sell =  Double.parseDouble(rate);
					}
					catch (Exception e) 
					{
						sell=0;
						e.printStackTrace();
					}
					
					//line = line + "\t" + rate;
					try {
						rs= DBManager.getSelect(query + " and BuySell="+BUY);
						rs.next();
						rate = rs.getString("rate");
						buy =  Double.parseDouble(rate);
					} 
					catch (Exception e) 
					{
						buy=0;
						e.printStackTrace();
					}
					//line = line + "\t" + rate;
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				finally
				{	
					raf1.writeBytes(line+"\t"+mrp+"\t"+sell+"\t"+buy+"\n");
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			
		} 
	}
	public static void main(String[] args) 
	{
		Addrate rate  = new Addrate();
		rate.AddRate();
	}
}
